package cn.itsource.basic.controller;

import cn.itsource.basic.service.IVerificationCodeService;
import cn.itsource.basic.utils.AjaxResult;
import cn.itsource.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 验证码Controller
 */
@RestController
@RequestMapping("/verification")
public class VerificationCodeController {

    @Autowired
    private IVerificationCodeService verificationCodeService;

    /**
     * 发送注册验证码
     * 验证码需求：
     *      1.后台随机产生4个字符
     *      2.1分钟以内只能发送1次验证码
     *      3.超过1分钟，但在5分钟以内，发送的验证码依然是第一次产生的验证码字符
     *      4.超过了5分钟以后，产生全新的验证码
     * @return
     */
    @PostMapping("/sendRegisterVerificationCode")
    public AjaxResult sendRegisterVerificationCode(@RequestBody User user){
        try {
            verificationCodeService.sendRegisterVerificationCode(user.getPhone());
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

    /**
     * 发送绑定验证码(微信和登录用户进行绑定)
     * @param user
     * @return
     */
    @PostMapping("/sendBinderVerificationCode")
    public AjaxResult sendBinderVerificationCode(@RequestBody User user){
        try {
            verificationCodeService.sendBinderVerificationCode(user.getPhone());
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }
}
